using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using System;
using UnityEngine.UI;
using UnityEngine.Video;

public class UISystem : MonoBehaviour
{
    public class EMPTYDEFINE : ScriptableObject { public GameObject GetPrefab(string name) { return null; } }
    public static Dictionary<string, UIInstance> uiPool;
    public static EMPTYDEFINE uiDefine;

    private static string defineName = "Assets/Poke Gloable UI Instance Define.asset";
    //这里确实要根据项目配置走不同逻辑了
    public static string definePath => "";


    [RuntimeInitializeOnLoadMethod]
    public static void LoadDefineData()
    {
        uiPool = new Dictionary<string, UIInstance>();
        uiDefine = LoadSystem.LoadAsset<EMPTYDEFINE>(defineName);
    }


    public static UIInstance Open(string instanceName, object param = null)
    {
        var prefab = uiDefine.GetPrefab(instanceName);
        return Open(prefab, param);
    }
    public static UIInstance Open(GameObject prefab, object param = null)
    {
        var instanceName = prefab.name;
        var go = GameObject.Instantiate(prefab);
        var containerBase = go.GetComponent<VGUI_ContainerBase>();

        if (containerBase == null)
        {
            DestroyImmediate(go);
            Debug.Log($"{prefab.name}这不是UI预制件, 请检查!");
            return null;
        }

        containerBase.Init(param);
        var instance = Regis(containerBase);
        go.name = instanceName;
        uiPool.TryAdd(instanceName, instance);

        SetLayer(instance);
        return instance;
    }

    public static void SetLayer(UIInstance instance)
    {
        var canvas = GameObject.Find("Canvas").transform;
        instance.transform.SetParent(canvas);
        instance.transform.localPosition = Vector3.zero;
    }

    public static UIInstance Regis(VGUI_ContainerBase container)
    {
        CreateBindings(container.bindings);
        return new UIInstance(container);
    }
    public static void UnRegis(VGUI_ContainerBase container)
    {
        CancelBindings(container.bindings);
    }
    private static void CreateBindings(List<UIBindTarget> bindings)
    {
        foreach (var binding in bindings)
            //binding.CreateBinding();
            binding.DoBind();
    }
    private static void CancelBindings(List<UIBindTarget> bindings)
    {
        foreach (var binding in bindings)
            binding.UnBind();
    }


    public class UIInstance
    {
        //private string version = "1.0";
        public VGUI_ContainerBase container;

        public Transform transform => container.transform;
        public UIInstance(VGUI_ContainerBase container)
        {
            this.container = container;
        }

        public Button GetButton(string btn)
        {
            foreach (var item in container.maunalNodes)
            {
                if (item.name.Equals(btn))
                    return item.GetComponent<Button>();
            }
            return null;
        }
    }
}


